import React, { useState } from 'react'
import { Button, Table, Space, Tag, ConfigProvider } from 'antd'
import { useTranslation } from 'react-i18next'
import type { ColumnsType } from 'antd/es/table'
// 引入antd内置的的语言包
import zhCN from 'antd/locale/zh_CN'
import enUS from 'antd/locale/en_US'

import Header from './Header'

import './App.css'

import { useAppSelector } from './app/hooks'

interface DataType {
  key: string
  name: string
  age: number
  address: string
  tags: string[]
}

const data: DataType[] = [
  {
    key: '1',
    name: 'John Brown',
    age: 32,
    address: 'New York No. 1 Lake Park',
    tags: ['nice', 'developer'],
  },
  {
    key: '2',
    name: 'Jim Green',
    age: 42,
    address: 'London No. 1 Lake Park',
    tags: ['loser'],
  },
  {
    key: '3',
    name: 'Joe Black',
    age: 32,
    address: 'Sydney No. 1 Lake Park',
    tags: ['cool', 'teacher'],
  },
]

function App() {
  const lang = useAppSelector((state) => {
    return state.zs.lang
  })
  const { t, i18n } = useTranslation(['app'])
  const columns: ColumnsType<DataType> = [
    {
      title: t('name'),
      dataIndex: 'name',
      key: 'name',
      render: (text) => <a>{text}</a>,
    },
    {
      title: t('age'),
      dataIndex: 'age',
      key: 'age',
    },
    {
      title: t('address'),
      dataIndex: 'address',
      key: 'address',
    },
    {
      title: t('tag'),
      key: 'tags',
      dataIndex: 'tags',
      render: (_, { tags }) => (
        <>
          {tags.map((tag) => {
            let color = tag.length > 5 ? 'geekblue' : 'green'
            if (tag === 'loser') {
              color = 'volcano'
            }
            return (
              <Tag color={color} key={tag}>
                {tag.toUpperCase()}
              </Tag>
            )
          })}
        </>
      ),
    },
    {
      title: t('action'),
      key: 'action',
      render: (_, record) => (
        <Space size="middle">
          <a>Invite {record.name}</a>
          <a>Delete</a>
        </Space>
      ),
    },
  ]
  return (
    <ConfigProvider
      // 国际化
      locale={lang === 'zh' ? zhCN : enUS}
    >
      <Header></Header>
      <hr />

      <h1>{t('helo')}</h1>
      <h1>{t('sz')}</h1>

      <hr />
      <Table
        columns={columns}
        dataSource={data}
        pagination={{
          total: 60,
          showQuickJumper: true,
          showSizeChanger: true,
        }}
      />
    </ConfigProvider>
  )
}

export default App
